using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Caramel.ServiceModel
{
	public class ReadOnlyUnitOfWorkCallContextInitializer : ICallContextInitializer
	{
		#region ICallContextInitializer Members

		public virtual object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
		{
			return UnitOfWork.Begin();
		}

		public virtual void AfterInvoke(object correlationState)
		{
			var work = correlationState as IUnitOfWork;

			if (work != null)
			{
				work.Dispose();
			}
		}

		#endregion
	}
}
